package com.kzw.tools;

import java.io.File;

import org.apache.commons.io.FileUtils;

/**
 * Mapping文件：
 * 		Oracle ==> MySQL (修改主键生成策略)
 * */
public class HbmOracle2MySQL {

	/**
	 * 递归扫描某目录及子目录下的.hbm.xml文件
	 * */
	public static void removeSequence(File file) throws Exception {
		if(file.getName().equals("web") || file.getName().startsWith(".")) return;
		
		if(file.isFile()) {
			String fname = file.getName();
			if(fname.endsWith(".hbm.xml")) {
				String content = FileUtils.readFileToString(file, "UTF-8");
				String newContent = content.replaceAll("<generator.*(\\r?\\n.*)*.*</generator>", "<generator class=\"native\"/>");
				if(!newContent.equals(content)) {
					System.out.println(file.getCanonicalPath());
					FileUtils.writeStringToFile(file, content, "UTF-8");
				}
			}
		} else {
			File[] fs = file.listFiles();
			for(File f : fs) {
				removeSequence(f);
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		String path = HbmOracle2MySQL.class.getResource("/").toURI().getPath();
		File srcPath = new File(path, "../../../").getCanonicalFile();
		System.out.println("处理根路径：" + srcPath.getCanonicalPath());
		
		removeSequence(srcPath);
	}
}
